package com.lanmon.controller.auth;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanmon.entity.Organization;
import com.lanmon.service.auth.OrganizationService;

@Controller
@RequestMapping("/auth")
public class OrganizationController {
	@Autowired
	private OrganizationService organizationService;
	
	@ResponseBody
	@RequestMapping("/allorgs")
	public List<Map<String, Object>> getOrgTree(String node) {
		Criterion criterion = null;
		if ("root".equals(node)) {
			criterion = Restrictions.isNull("organization.orgid");
		}
		else {
			criterion = Restrictions.eq("organization.orgid", Integer.parseInt(node));
		}
		List<Map<String, Object>> list = new ArrayList<>();
		List<Organization> tempList = organizationService.findObjects(criterion);
		for (int i = 0; i < tempList.size(); i++) {
			Map<String, Object> map = new HashMap<>();
			Organization org = tempList.get(i);
			map.put("id", org.getOrgid());
			map.put("text", org.getOrgname());
			map.put("leaf", false);
			map.put("expanded", true);
			list.add(map);
		}
		return list;
	}
}
